package com.scpii.api.controller.user;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.scpii.api.common.annoation.ActionTypeParameter;
import com.scpii.api.common.annoation.Pagination;
import com.scpii.api.domain.user.UserActions;
import com.scpii.api.domain.user.UserActions.ActionType;
import com.scpii.api.domain.user.UserShare;
import com.scpii.api.service.user.UserActionsService;
import com.scpii.api.util.PrincipalUtil;

@Controller
@RequestMapping("/content/user_actions")
public class UserActionsController {

	@Resource
	private UserActionsService userActionsService;

	@PreAuthorize("hasRole('ROLE_CLIENT')")
	@RequestMapping(value = "/target/{targetId}", method = RequestMethod.GET)
	@ResponseBody
	@Pagination
	public List<UserActions> getUserActionsByTargetIdAndActionTypes(
			@PathVariable("targetId") Long targetId,
			@RequestParam(value = "actionTypes", required = false) String actionTypes) {
		return userActionsService.selectByTargetAndActionTypes(targetId,
				StringUtils.split(actionTypes, ","));
	}

	@PreAuthorize("hasRole('ROLE_CLIENT')")
	@RequestMapping(value = "/user/status/{targetId}", method = RequestMethod.GET)
	@ResponseBody
	public List<UserActions> getUserActionsStatus(
			@PathVariable Long targetId,
			@RequestParam(value = "actionTypes", required = false) String actionTypes) {
		return userActionsService.selectUserStatusByTargetAndActionTypes(
				PrincipalUtil.getPrincipal().getId(), targetId,
				StringUtils.split(actionTypes, ","));
	}

	@PreAuthorize("hasRole('ROLE_CLIENT')")
	@RequestMapping(value = "/user/status", method = RequestMethod.GET)
	@ResponseBody
	@Pagination
	public List<UserActions> getUserActionsStatusByType(
			@RequestParam(value = "actionTypes", required = false) String actionTypes) {
		return userActionsService.selectByUserIdAndActionType(PrincipalUtil
				.getPrincipal().getId(), StringUtils.split(actionTypes, ","));
	}

	@PreAuthorize("hasRole('ROLE_CLIENT')")
	@RequestMapping(value = "/user/target/{targetId}", method = RequestMethod.GET)
	@ResponseBody
	public List<UserActions> getUserActionsUserByTargetId(UserActions record) {
		record.setCreateBy(PrincipalUtil.getPrincipal().getId());
		return userActionsService.selectBySelectiveWithSingle(record);
	}

	@PreAuthorize("hasRole('ROLE_CLIENT')")
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	@ActionTypeParameter
	public Boolean addUsersActions(@RequestParam("targetId") Long targetId,
			@RequestParam("actionType") String actionType,
			UserActions userActions, UserShare userShare) {
		if (ActionType.ACTIVITIES_SHARE.getValue().equals(actionType)) {
			return true;
		}
		userActions.setTargetId(targetId);
		userActions.setActionType(actionType);
		userActions.setCreateBy(PrincipalUtil.getPrincipal().getId());
		userActions.setUserId(PrincipalUtil.getPrincipal().getId());
		List<UserActions> userActionList = userActionsService
				.selectBySelective(userActions);
		return userActionList.size() > 0 ? Boolean.FALSE : Boolean.TRUE;
	}

	@PreAuthorize("hasRole('ROLE_CLIENT')")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public Boolean deleteUsersActions(@PathVariable("id") Long id) {
		userActionsService.deleteByPrimaryKey(id);
		return true;
	}

}
